package demiBoard;



import java.util.ArrayList;
import java.util.HashMap;

import dataBase.DBManager;

public class DemiBoard {
	DBManager boardDM;
		
	public  DemiBoard(){
		boardDM=DBManager.getDbm();
	}
	/*
	 * 글쓰기 
	 */
	public void postBoard(int cBoardId,String cPostName,String cPostData,String cAuthor,int cMother){
		long cPostDate=System.currentTimeMillis ( ); 
		/*글쓰기일때*/
		if(cMother==0){
			boardDM.excuteUpdate(
					"INSERT " +
					"tdemiboard(cBoardId, cPostName, cPostData, cPostDate, cAuthor, cAlign, cMother,cDepth) " +
					"select "+cBoardId+", '"+cPostName+"', '"+cPostData+"', "+cPostDate+", '"+cAuthor+"',IFNULL((select max(cAlign)+1 from tdemiboard),0),"+ cMother+",0;");
		}
		/*답글달기*/
		else{
			boardDM.createStatement();
				boardDM.addBatch("set @cAlign=IFNULL((select cAlign from tdemiboard where cPostId = "+cMother+"),0);");
				boardDM.addBatch("set @cDepth=IFNULL((select IFNULL(cDepth,0)+1 from tdemiboard where cPostId = "+cMother+"),0);");
				
				boardDM.addBatch("update tdemiboard set cAlign=cAlign+1 where cAlign >=@cAlign");
				boardDM.addBatch("INSERT " +
					"tdemiboard(cBoardId, cPostName, cPostData, cPostDate, cAuthor, cAlign, cMother,cDepth) " +
					"select "+cBoardId+", '"+cPostName+"', '"+cPostData+"', "+cPostDate+", '"+cAuthor+"',@cAlign,"+ cMother+",@cDepth;");
				
			boardDM.executeBatch();
			boardDM.closeStmt();
		}
	}
	/*
	 * 목록 보여주기
	 */
	public ArrayList getList(int cBoardId,int pageNum,int listCount){
		ArrayList returnList=null;
		
		int startNum=(pageNum-1)*listCount;
		returnList=boardDM.getQueryDataArayList("select * from (select *,@rownum:=(@rownum+1) rownum from tdemiboard  A,(select @rownum:=0) B where A.cBoardId="+cBoardId+" order by A.cAlign desc) C where C.rownum>"+startNum+" limit "+listCount);
		
		return returnList;		
	}
	/*
	 * 페이지 겟수
	 */
	public int getPageNumOfPage(int cBoardId,int listCount){
		int pageNum;
		HashMap tempMap;
		tempMap=boardDM.getOneData("select ceil(count(*)/"+listCount+") numPage from tdemiboard where cBoardId="+cBoardId);
		pageNum=Integer.parseInt((String)tempMap.get("numPage"));
		return pageNum;
	}
	/*
	 * 총글수
	 */
	public int getNumOfPost(int cBoardId){
		int pageNum;
		HashMap tempMap;
		tempMap=boardDM.getOneData("select count(*) numPage from tdemiboard where cBoardId="+cBoardId);
		pageNum=Integer.parseInt((String)tempMap.get("numPage"));
		return pageNum;
	}
	
	
	/*
	 * 페이지 설정 가져오기
	 */
	public HashMap getBoardProper(int boardId){
		HashMap returnMap;
		returnMap=boardDM.getOneData("SELECT cBoardName, cBoardPerPostNum, cBoardPerPageNum FROM tdemiboardlist where cBoardId="+boardId);
		return returnMap;
	}

	
	/*
	 * 글보기
	 */
	public HashMap getPostData(int cPostId){
		ArrayList tempList;
		
		tempList=boardDM.getQueryDataArayList("select * from tdemiboard where cPostId="+cPostId);
		
		return (HashMap)tempList.get(0);
	}
	/*
	 * 게시판 추가
	 */
	public void addBoard(String cBoardName,int cBoardPostNumber,int cBoardPageNumber){
		boardDM.excuteUpdate("insert into tdemiboardList(cBoardName,cBoardPerPostNum,cBoardPerPageNum) " +
				"value('"+cBoardName+"',"+cBoardPostNumber+","+cBoardPageNumber+")");
	}
	/*
	 * 게시판 삭제
	 */
	public void deleteBoard(int cBoardId){
		boardDM.excuteUpdate("delete from tdemiboardList where cBoardId="+cBoardId);
	}
	/*
	 * 게시판 수정
	 */
	public void modifyBoard(int cBoardId,String cBoardName,int cBoardPostNumber,int cBoardPageNumber){
		boardDM.excuteUpdate("update tdemiboardList set cBoardName='"+cBoardName+"'" +
				",cBoardPerPostNum="+cBoardPostNumber+
				",cBoardPerPageNum="+cBoardPageNumber+
				" where cBoardId="+cBoardId);
	}
	/*
	 *게시판 목록 보여주기
	 */
	public ArrayList getBoardList(){
		ArrayList returnList=null;
		returnList=boardDM.getQueryDataArayList("select cBoardId, cBoardName, cBoardPerPostNum, cBoardPerPageNum from tdemiboardList");
		return returnList;		
	}
	
	
}
